//
//  MainTabbar.swift
//  sinaOnceAgain
//
//  Created by 王相超 on 16/2/16.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {

    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.addSubview(plusBtn)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局子视图的方法
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //确定tabbarButton的大小
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个 能够递增的索引
        var index: CGFloat = 0
        
        //遍历子视图  获取UITbarButton
        for subview in subviews {
            
            //保证 通过UITabBarButton 字符串 可以获取相对应的类型
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                //修改frame
                subview.frame = CGRectOffset(rect, w * index, 0)
                
                index += (index == 1 ? 2 : 1)
                
            }
        }
        
        //设置加号按钮的位置
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    
    lazy var plusBtn: UIButton = UIButton(backImageName: "tabbar_compose_button", imageName: "tabbar_compose_icon_add")
    
  
}